package com.ytkj.tradeserver.model;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/3/19.
 */
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private long total; //总记录数
    private int pageNum = 1; //第几页
    private int pageSize = 2; //每页记录数
    private int pages; // 总页数
    private int size; //当前页的数量<=pageSize
    private int pageIndexCount = 10;//需要显示的页数
    private int[] indexs;
    private String pageStr;
    private String url;
    private List<T> list; //结果集

    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getPageNum() {
        return pageNum;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPages() {
        return pages;
    }
    public void setPages(int pages) {
        this.pages = pages;
    }
    public String getPageStr() {
        return pageStr;
    }

    public PageBean() {
    }

    public PageBean(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageBean(List<T> list, String url){
        if (list instanceof Page){
            Page<T> page = (Page<T>) list;
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
            this.setPageNum(page.getPageNum());
            this.url = url;
            this.setPageStr("");
        }
    }



    public void setPageNum(int pageNum) {
        if(pageNum >= this.pages) {
            this.pageNum = this.pages;
        } else if(pageNum <= 0) {
            this.pageNum = 1;
        } else {
            this.pageNum = pageNum;
        }
        if(this.getPageIndexCount() > this.pages) {
            this.setPageIndexCount(this.pages);
        }
        this.indexs = new int[this.getPageIndexCount()];
        int istart = this.pageNum - this.getPageIndexCount() / 2 + (this.getPageIndexCount() % 2 > 0?0:1);
        int iend = this.pageNum + this.getPageIndexCount() / 2;
        if(istart <= 0) {
            istart = 1;
            iend = this.getPageIndexCount();
        }

        if(iend > this.pages) {
            iend = this.pages;
            istart = this.pages - this.getPageIndexCount() + 1;
        }

        for(int i = 0; i < iend - istart + 1; ++i) {
            this.indexs[i] = istart + i;
        }

    }



    public void setPageStr(String pageStr) {
        StringBuffer sb = new StringBuffer();
        if(this.total > this.pageSize){
            String urlStr = this.url;
            if( this.url.indexOf("?")==-1 ){
                urlStr += "?";
            }else if( this.url.indexOf("?")!=-1 && !this.url.endsWith("?")){
                urlStr += "&";
            }

            sb.append("<ul id='page-ul'>");
            sb.append("<li><a>共<span class=\'totalcount\'>" + this.total + "</span>条记录</a></li>");
            sb.append("<li><a href=\'" + urlStr + "pageNum=1&pageSize="+this.pageSize+"\' target=\'_self\'>首页</a></li>");
            sb.append("<li><a href=\'" + urlStr + "pageNum=" + this.getPreviousIndex() + "&pageSize=" + this.pageSize + "\' target=\'_self\'>上一页</a></li>");

            for(int i = 0; i < this.getIndexs().length; ++i) {
                if(this.pageNum == this.getIndexs()[i]) {
                    sb.append("<li><a href=\'javascript:void(0)\' class=\'active\'>" + this.getIndexs()[i] + "</a></li>");
                } else {
                    sb.append("<li><a href='" + urlStr + "pageNum=" + this.getIndexs()[i] + "&pageSize=" + this.pageSize + "\' target=\'_self\'>" + this.getIndexs()[i] + "</a></li>");
                }
            }

            sb.append("<li><a href=\'" + urlStr + "pageNum=" + this.getNextIndex() + "&pageSize=" + this.pageSize + "\' target=\'_self\'>下一页</a></li>");
            sb.append("<li><a href=\'" + urlStr + "pageNum=" + this.pages + "\' target=\'_self\'>末页</a></li>");
            sb.append("<li><a> 页次：<span class=\'totalpage\'><span class=\'curpage\'>" + this.pageNum + "</span>/" + this.pages + "</span>页</a></li>");
            sb.append("<li><a><span class=\'numpage\'>" + this.pageSize + "</span>条/页</a></li>");
            sb.append("</ul>");
        }
        this.pageStr = "N".equals(pageStr)?null:sb.toString();
    }

    public int getNextIndex() {
        int nextIndex = this.getPageNum() + 1;
        return nextIndex > this.pages?this.pages:nextIndex;
    }

    public int getPreviousIndex() {
        int previousIndex = this.pageNum - 1;
        return previousIndex <= 0?1:previousIndex;
    }

    public int getPageIndexCount() {
        return pageIndexCount;
    }

    public void setPageIndexCount(int pageIndexCount) {
        this.pageIndexCount = pageIndexCount;
    }

    public int[] getIndexs() {
        return indexs;
    }

    public void setIndexs(int[] indexs) {
        this.indexs = indexs;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
